﻿#include "GraphicsAirValveItem.h"
#include <QBrush>
#include <QPen>
#include <QPainter>
#include <QDebug>
#define _USE_MATH_DEFINES
#include <cmath>

#define M_PI       3.14159265358979323846

GraphicsAirValveItem::GraphicsAirValveItem()
{
    m_pointsDown[0] = QPoint(0, 15);
    m_pointsDown[1] = QPoint(-17,-15);
    m_pointsDown[2] = QPoint(17,-15);

    m_pointsUp[0] = QPoint(0, -15);
    m_pointsUp[1] = QPoint(-17, 15);
    m_pointsUp[2] = QPoint(17, 15);

    m_pointsLeft[0] = QPoint(-15, 0);
    m_pointsLeft[1] = QPoint(15, 17);
    m_pointsLeft[2] = QPoint(15, -17);

    m_pointsRight[0] = QPoint(15, 0);
    m_pointsRight[1] = QPoint(-15, -17);
    m_pointsRight[2] = QPoint(-15, 17);

    m_width = 51; // 默认箭头向右指
    m_height = 35;

    m_lineH[0] = QLine(-17, -8, 17, -8);
    m_lineH[1] = QLine(-17, 8, 17, 8);

    m_lineV[0] = QLine(-8, -17, -8, 17);
    m_lineV[1] = QLine(8, -17, 8, 17);


    m_dir = Right;
}

QPointF GraphicsAirValveItem::keyPoint(int i)
{
    switch(m_dir)
    {
    case Up:
        return i == 0 ? QPointF(0, 25 * scale()) : QPointF(0, -25 * scale());
        break;
    case Down:
        return i == 0 ? QPointF(0, -25 * scale()) : QPointF(0, 25 * scale());
        break;
    case Left:
        return i == 0 ? QPointF(25 * scale(), 0) : QPointF(-25 * scale(), 0);
        break;
    case Right:
        return i == 0 ? QPointF(-25 * scale(), 0) : QPointF(25 * scale(), 0);
        break;
    }


    return QPointF();
}

QRectF GraphicsAirValveItem::boundingRect() const
{
    switch(m_dir)
    {
    case GraphicsAirValveItem::Left:
    case GraphicsAirValveItem::Right:
        return QRectF(-26 * scale() , -18 * scale() , 53 * scale() , 37 * scale() );
        break;
    default:
        return QRectF(-18 * scale() , -26 * scale() , 37 * scale() , 53 * scale() );
    }
}

void GraphicsAirValveItem::setDir(Direction dir)
{
    m_dir = dir;
    switch(m_dir)
    {
    case Left:
    case Right:
        m_width = 51;
        m_height = 35;
        break;
    case Up:
    case Down:
        m_width = 35;
        m_height = 51;
        break;
    }
    update(QRect(-26, 26, 53, 53));
}

void GraphicsAirValveItem::setEnable(bool enable)
{
    m_enable = enable;
    update();
}

void GraphicsAirValveItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    m_enable = !m_enable;
    emit valveStateChanged(m_enable);
    update();
}

void GraphicsAirValveItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPen penOn(Qt::black), penOff(Qt::red);
    penOn.setWidth(3);
    penOff.setWidth(3);

    QBrush brushOn(Qt::green);

    if(m_enable)
    {
        drawStateOn(painter, m_dir);
    }
    else
    {
        drawStateOff(painter, m_dir);
    }
}

void GraphicsAirValveItem::drawStateOn(QPainter *painter, Direction dir)
{
    QPen penOn(Qt::black);
    penOn.setWidth(3);
    QBrush brushOn(Qt::green);
    painter->setBrush(brushOn);
    painter->setPen(penOn);
    switch (dir) // 画连接线
    {
    case GraphicsAirValveItem::Left:

        painter->drawPolygon(m_pointsLeft, 3);
        painter->drawLine(QLine(-24, 0, -16, 0));
        painter->drawLine(QLine(24, 0, 16, 0));
        break;
    case GraphicsAirValveItem::Right:
        painter->drawPolygon(m_pointsRight, 3);
        painter->drawLine(QLine(-24, 0, -16, 0));
        painter->drawLine(QLine(24, 0, 16, 0));
        break;
    case GraphicsAirValveItem::Down:
        painter->drawPolygon(m_pointsDown, 3);
        painter->drawLine(QLine(0, -24, 0, -16));
        painter->drawLine(QLine(0, 24, 0, 16));
        break;
    case GraphicsAirValveItem::Up:
        painter->drawPolygon(m_pointsUp, 3);
        painter->drawLine(QLine(0, -24, 0, -16));
        painter->drawLine(QLine(0, 24, 0, 16));
        break;
    }
}

void GraphicsAirValveItem::drawStateOff(QPainter *painter, Direction dir)
{
    QPen penOn(Qt::black), penOff(Qt::red);
    penOn.setWidth(3);
    penOff.setWidth(3);
    switch (dir)
    {
    case GraphicsAirValveItem::Left:
    case GraphicsAirValveItem::Right:
        painter->setPen(penOff);
        painter->drawLine(m_lineV[0]);
        painter->drawLine(m_lineV[1]);
        painter->setPen(penOn);
        painter->drawLine(QLine(-24, 0, -9, 0));
        painter->drawLine(QLine(24, 0, 9, 0));
        break;

    case GraphicsAirValveItem::Down:
    case GraphicsAirValveItem::Up:
        painter->setPen(penOff);
        painter->drawLine(m_lineH[0]);
        painter->drawLine(m_lineH[1]);
        painter->setPen(penOn);
        painter->drawLine(QLine(0, -24, 0, -9));
        painter->drawLine(QLine(0, 24, 0, 9));
        break;
    }
}
